package com.zt.service.impl;

import com.zt.globalExceptionHandler.BizException;
import com.zt.mapper.DeptMapper;
import com.zt.pojo.Dept;
import com.zt.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
//服务层: 实现业务逻辑,调用Mapper,返回结果
@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;
    //全部查询
    @Override
    public List<Dept> findAll() {
        List<Dept> deptslist = deptMapper.findAll();
        return deptslist;
    }
    //通过id删除
    @Override
        public void deleteById(Integer id) {
        //当部门下有员工时,不能删除
        Integer num = deptMapper.findempById(id);
        if(num > 0){
            throw new BizException("部门下有员工,不能删除");
        }else {
            deptMapper.deleteById(id);
        }
    }
    //添加
    @Override
    public void add(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(dept.getCreateTime());
        deptMapper.add(dept);
    }
    //通过id查询
    @Override
    public Dept findById(Integer id) {
        Dept dept = deptMapper.findById(id);
        return dept;
    }
    //修改
    @Override
    public void update(Dept dept) {
        //更新时间
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.update(dept);
    }
}
